package com.winit.deploy.task.springBootTask;

import com.winit.core.server.base.CmdOutputHandler;
import com.winit.core.task.Task;
import com.winit.deploy.bean.WinitSpringBootInfo;
import com.winit.deploy.server.WinitSpringBootServer;
import com.winit.deploy.util.DeployFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjian on 16/8/10.
 * 发布任务step3 备份
 */
public class SpringBootBakTask extends Task {

    static final Logger logger = LoggerFactory.getLogger(SpringBootBakTask.class);

    public WinitSpringBootServer server;

    public SpringBootBakTask(String taskId, Integer expireTime, WinitSpringBootServer server) {
        super(taskId, expireTime);
        this.server = server;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.springBootTask.SpringBootBakTask#call start taskId=" + this.taskId);
        final TaskStatus[] taskStatus = {TaskStatus.STATUS_SUCCESS};
        CmdOutputHandler handler = new CmdOutputHandler() {

            @Override
            public void onMessage(String cmd, int index, String message, Boolean isErrorMessage) {
                if (isErrorMessage) {
                    taskStatus[0] = TaskStatus.STATUS_ERROR;
                } else {
                    taskStatus[0] = TaskStatus.STATUS_SUCCESS;
                }
                logger.info("SpringBootBakTask:执行备份,isErrorMessage=" + isErrorMessage + ",message=" + message);
            }
        };
        this.server.setCmdOutputHandler(handler);
        this.server.doBakUp();
        WinitSpringBootInfo info = this.server.getInfo();
        DeployFileUtil.saveBackPackagePath(info.getEnvName(), info.getAreaName(), this.taskId, info.getBackUpPath());
        logger.info("com.winit.deploy.task.springBootTask.SpringBootBakTask#call end taskId=" + this.taskId);
        return taskStatus[0];
    }

    public WinitSpringBootServer getServer() {
        return this.server;
    }
}
